<?php
namespace app\api\controller\robat;

use think\Controller;
use app\common\model\user\User;
use app\api\controller\mytool\Mytool;
use app\api\controller\Base;
use app\api\controller\Pub;
use app\common\model\sendmsg\Sendmsg;
use app\common\model\sendmsg\Sendmsglog;
use app\api\controller\mytool\Sendmessage as Sm;
use app\common\model\orderdata\Goods;
use app\common\model\quotation\QuotationList;
use app\common\model\company\Contract;
use app\common\model\cost\Costjournal;
use app\common\model\orderhawa\HawaList;
use app\common\model\orderdelivery\DeliveryList;
use app\common\model\orderreceipt\ReceiptList;
use app\common\model\company\Company as Companymodel;
use app\api\controller\mytool\Mapapi;
class Company extends Base
{
    /**
     * 将没有定位的企业获取定位
     * @return [type] [description]
     */
    public function getcompanylocaltion(){
        $data = Companymodel::whereNull('Lat')->where('Address','not like','%（经纬度可信性')->find();
        // $data = Companymodel::where('Address','like','%（经纬度可信性较低）')->find();
        // $data['Address'] = substr($data['Address'],0,strpos($data['Address'],"（经纬度可信性较低）"));
        if(empty($data['Address'])){$data['Address'] = '';}
        $add = $data['Address'] . $data['Name'];
        $err = false;
        // dump($data);
        if(!empty($add)){
            echo '正在获取'.$add.'请稍等';
            $map = new Mapapi;
            $rdata  = $map->getlocaltion(urlencode($add));
            // dump($rdata);
            if($rdata['status']==0 && $rdata['result']['reliability']>=9){
                $data->Lat = $rdata['result']['location']['lat'];
                $data->Lon = $rdata['result']['location']['lng'];
                $data->Address = $data['Address'];
                $data->save();
                echo "<script>location.reload();</script>";
            }elseif($rdata['status']==0){
                $data->Lat = $rdata['result']['location']['lat'];
                $data->Lon = $rdata['result']['location']['lng'];
                $data->Address = $data->Address ."（经纬度可信性".$rdata['result']['reliability']."级）"; 
                $data->save();
                echo "<script>location.reload();</script>";
            }else{
                switch ($rdata['status']) {
                    case '120':
                        echo "每秒已达上限";
                        sleep(1);
                        echo "<script>location.reload();</script>";
                        break;
                    case '121':
                        echo "每日已达上限";
                        break;
                    default:
                        if($rdata['message']=='查询无结果'){
                            $data->Lat = 0;
                            $data->Address = $data['Address'] ."（经纬度可信性较低）【没有找到此定位】"; 
                            echo "<script>location.reload();</script>";
                            $data->save();
                            echo "<script>location.reload();</script>";
                        }else{
                            echo $rdata['message'];
                        }
                        // $data->RobatMap = -1;
                        // $data->save();
                        // echo "<script>location.reload();</script>";
                        break;
                }
            }
        }else{
            // $data->RobatMap = -1;
            $data->save();
            echo "<script>location.reload();</script>";
        }
        
    }

   
}
